package com.ffs.game.scene;

import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.ffs.game.scene.obj.Monster;
import com.ffs.persist.utils.ExecutorUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;


/**
 * 关卡场景
 *
 * @author ffs80
 */
public abstract class GameScene implements Screen {

    /**
     * 纹理图片（背景）
     */
    private Texture texture;

    /**
     * 舞台
     */
    private Stage stage;

    /**
     * 关卡id
     */
    private int sceneId;

    /**
     * 宽度
     */
    private int width;

    /**
     * 高度
     */
    private int height;

    /**
     * 下一场景
     */
    private GameScene nextScene;

    /**
     * 定时管理
     */
    private ScheduledFuture<?> scheduledFuture;

    /**
     * 场景中所有的怪物
     */
    private Map<Long, Monster> monsterMap = new HashMap<>();


    public GameScene(Stage stage, int sceneId) {
        this.stage = stage;
        this.sceneId = sceneId;
    }

    public void addActor(Actor actor) {
        stage.addActor(actor);
        if (actor instanceof Monster) {
            Monster monster = (Monster) actor;
            monsterMap.put(((Monster) actor).getId(), monster);
        }
    }

    public void update(int delta) {
        monsterHeart(delta);
    }

    /**
     * 怪物死亡
     */
    public void onMonsterDie(Monster monster) {

    }

    /**
     * 怪物复活
     */
    public void onMonsterBorn(Monster monster) {

    }

    private void monsterHeart(int delta) {
        if (monsterMap.isEmpty()) {
            return;
        }

        Monster[] monsters = monsterMap.values().toArray(new Monster[0]);
        for (Monster monster : monsters) {
            monster.getMonsterMachine().update(this, delta);
        }
    }

    public void initDriver() {
        SceneHeartEvent sceneHeartEvent = new SceneHeartEvent(this);
        scheduledFuture = ExecutorUtil.SCENE_TIMER.scheduleAtFixedRate(sceneHeartEvent, 0, 100, TimeUnit.MILLISECONDS);
    }

    public void stopTimer() {
        scheduledFuture.cancel(false);
    }

    public void draw() {
        stage.draw();
    }

    /**
     * 场景被设置到主场景时调用
     */
    @Override
    public void show() {
        System.out.println("show");
    }

    /**
     * 场景需要被渲染时调用
     */
    @Override
    public void render(float v) {

    }

    /**
     * 重新设置场景大小
     */
    @Override
    public void resize(int i, int i1) {

    }

    /**
     * 暂停时调用
     */
    @Override
    public void pause() {

    }

    /**
     * 从暂停中恢复时调用
     */
    @Override
    public void resume() {

    }

    /**
     * 从主场景中退出时调用
     */
    @Override
    public void hide() {
        System.out.println("hide");
    }

    /**
     * 资源释放
     */
    @Override
    public void dispose() {

    }

    public int getSceneId() {
        return sceneId;
    }

    public void setSceneId(int sceneId) {
        this.sceneId = sceneId;
    }
}
